import os
from pathlib import Path
from dotenv import load_dotenv

# 1. 正确定位项目根目录：
#    settings.py 路径：项目根目录/config/settings.py
#    所以项目根目录 = config文件夹的父目录
BASE_DIR = Path(__file__).resolve().parent.parent  # 关键修正：多上溯一级

# 2. 显式加载项目根目录下的.env文件
env_path = BASE_DIR / ".env"  # 现在路径为：项目根目录/.env
if not env_path.exists():
    raise FileNotFoundError(f".env文件不存在，请检查路径：{env_path}")
load_dotenv(env_path)  # 加载正确路径的.env

# ----------------------- 火山大模型配置 -----------------------
VOLCANO_API_KEY = os.getenv("VOLCANO_API_KEY")
VOLCANO_API_URL = os.getenv("VOLCANO_API_URL", "https://ark.cn-beijing.volces.com/api/v3/chat/completions")
VOLCANO_MODEL_NAME = os.getenv("VOLCANO_MODEL_NAME", "doubao-seed-1-6-250615")

# ------------------------- 飞书配置 -------------------------
FEISHU_APP_ID = os.getenv("FEISHU_APP_ID")
FEISHU_APP_SECRET = os.getenv("FEISHU_APP_SECRET")
GRAPHIC_OUTLINE_TEMPLATE_SPREADSHEET_TOKEN = os.getenv("GRAPHIC_OUTLINE_TEMPLATE_SPREADSHEET_TOKEN")
GRAPHIC_OUTLINE_TEMPLATE_URL = os.getenv("GRAPHIC_OUTLINE_TEMPLATE_URL")

# ----------------------- zxy_get配置 -----------------------
ZXY_API_BASE_URL = os.getenv("ZXY_API_BASE_URL")  # 加载zxy基础URL
ZXY_API_SECRET = os.getenv("ZXY_API_SECRET")      # 加载zxy认证密钥

# ----------------------- 本地达人路径 -----------------------
LOCAL_INFLUENCER_PATH = os.getenv("LOCAL_INFLUENCER_PATH")

# ----------------------- 路径配置（非入参，保留） -----------------------
DEFAULT_PPT_PATH = os.getenv("PPT_PATH", str(BASE_DIR / "input" / "九牧轻智能马桶brief.pptx"))
DEFAULT_VIDEO_OUTLINE_PATH = os.getenv("VIDEO_OUTLINE_PATH", str(BASE_DIR / "input" / "视频大纲.txt"))
DEFAULT_OUTPUT_DIR = os.getenv("OUTPUT_DIR", str(BASE_DIR / "output"))
DEFAULT_IMAGE_DIR = os.getenv("IMAGE_DIR", str(BASE_DIR / "downloaded_images"))

# ----------------------- API配置（非入参，保留） -----------------------
API_HOST = os.getenv("API_HOST", "0.0.0.0")
API_PORT = int(os.getenv("API_PORT", "8000"))

# ----------------------- 确保目录存在 -----------------------
for directory in [DEFAULT_OUTPUT_DIR, DEFAULT_IMAGE_DIR, BASE_DIR / "input"]:
    os.makedirs(directory, exist_ok=True)

# ----------------------- 强制校验关键配置（可选） -----------------------
required_keys = [
    "FEISHU_APP_ID", "FEISHU_APP_SECRET",
    "GRAPHIC_OUTLINE_TEMPLATE_SPREADSHEET_TOKEN", "GRAPHIC_OUTLINE_TEMPLATE_URL",
    "LOCAL_INFLUENCER_PATH",
    "ZXY_API_BASE_URL", "ZXY_API_SECRET"  # 新增zxy配置的必填校验
]
for key in required_keys:
    if not globals().get(key):
        raise EnvironmentError(f"环境变量 {key} 未设置，请检查.env文件")


LOG_LEVEL = "INFO"
MAX_LOG_FILES = 30  # 最大日志文件数量
LOG_FILE_MAX_SIZE = 10  # 单个日志文件最大大小(MB)